package com.panda.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.panda.common.core.domain.model.LoginUser;
import com.panda.common.utils.DateUtils;
import com.panda.common.utils.ServletUtils;
import com.panda.common.utils.spring.SpringUtils;
import com.panda.framework.web.service.TokenService;
import com.panda.system.common.constant.Constants;
import com.panda.system.common.constant.EnumConstants;
import com.panda.system.domain.PandaTaskDetail;
import com.panda.system.domain.PandaWorkbench;
import com.panda.system.domain.SysNotice;
import com.panda.system.dto.UserTodoReportDto;
import com.panda.system.mapper.PandaWorkbenchMapper;
import com.panda.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 工作台Service业务层处理
 * 
 * @author panda
 * @date 2021-01-11
 */
@Service
public class PandaWorkbenchServiceImpl implements IPandaWorkbenchService 
{
    private static final Logger log = LoggerFactory.getLogger(IPandaWorkbenchService.class);
    @Autowired
    private PandaWorkbenchMapper pandaWorkbenchMapper;

    @Autowired
    private IPandaTaskDetailService taskDetailService;

    @Autowired
    private IPandaTaskService taskService;

    @Autowired
    private ISysNoticeService noticeService;

    @Autowired
    private ISysUserService iSysUserService;
    /**
     * 查询工作台
     * 
     * @param id 工作台ID
     * @return 工作台
     */
    @Override
    public PandaWorkbench selectPandaWorkbenchById(Long id)
    {
        return pandaWorkbenchMapper.selectPandaWorkbenchById(id);
    }

    /**
     * 查询工作台列表
     * 
     * @param pandaWorkbench 工作台
     * @return 工作台
     */
    @Override
    public List<PandaWorkbench> selectPandaWorkbenchList(PandaWorkbench pandaWorkbench)
    {
        try {
            LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
            if(loginUser!=null&&!"admin".equals(loginUser.getUsername())){
                pandaWorkbench.setUserId(loginUser.getUser().getUserId());
            }
        }catch (Exception e){
            log.info("获取用户信息失败！");
        }
        return pandaWorkbenchMapper.selectPandaWorkbenchList(pandaWorkbench);
    }

    /**
     * 新增工作台
     * 
     * @param pandaWorkbench 工作台
     * @return 结果
     */
    @Override
    public int insertPandaWorkbench(PandaWorkbench pandaWorkbench)
    {
        pandaWorkbench.setCreateTime(DateUtils.getNowDate());
        return pandaWorkbenchMapper.insertPandaWorkbench(pandaWorkbench);
    }

    /**
     * 修改工作台
     * 
     * @param pandaWorkbench 工作台
     * @return 结果
     */
    @Override
    public int updatePandaWorkbench(PandaWorkbench pandaWorkbench)
    {
        pandaWorkbench.setUpdateTime(DateUtils.getNowDate());
        return pandaWorkbenchMapper.updatePandaWorkbench(pandaWorkbench);
    }

    /**
     * 批量删除工作台
     * 
     * @param ids 需要删除的工作台ID
     * @return 结果
     */
    @Override
    public int deletePandaWorkbenchByIds(Long[] ids)
    {
        return pandaWorkbenchMapper.deletePandaWorkbenchByIds(ids);
    }

    /**
     * 删除工作台信息
     * 
     * @param id 工作台ID
     * @return 结果
     */
    @Override
    public int deletePandaWorkbenchById(Long id)
    {
        return pandaWorkbenchMapper.deletePandaWorkbenchById(id);
    }

    @Override
    public PandaTaskDetail createTaskDetail(Long todoId) {
        //查询todo
        PandaWorkbench workbench = pandaWorkbenchMapper.selectPandaWorkbenchById(todoId);
        PandaTaskDetail pandaTaskDetail = new PandaTaskDetail();
        pandaTaskDetail.setTaskId(workbench.getLinkId());
        pandaTaskDetail.setStatus(EnumConstants.TaskDetailStatus.NEW_CREATE.getCode());
        List<PandaTaskDetail> pandaTaskDetails = taskDetailService.selectPandaTaskDetailList(pandaTaskDetail);
        // 查询是否有新建任务完成明细
        if(!CollectionUtils.isEmpty(pandaTaskDetails)){
            PandaTaskDetail taskDetail = pandaTaskDetails.get(0);
            if(taskDetail != null){
                return taskDetail;
            }
        }
        // 判断是否还有待处理任务量
        if(taskService.checkTodoTask(workbench.getLinkId())){
            return taskDetailService.createTaskDetail(workbench.getLinkId());
        }
        return new PandaTaskDetail();
    }

    @Override
    public void shutDownTodo(PandaWorkbench pandaWorkbench) {
        pandaWorkbench.setStatus(EnumConstants.SysStatus.DOWN.getCode());
        pandaWorkbenchMapper.updatePandaWorkbench(pandaWorkbench);
    }

    @Override
    public void createTodoNotice(PandaWorkbench todo) {
        SysNotice notice =new  SysNotice();
        notice.setNoticeType(Constants.USER_NOTICE);
        notice.setNoticeContent(todo.getName());
        notice.setNoticeTitle(todo.getName());
        notice.setUserId(todo.getUserId());
        noticeService.insertNotice(notice);
    }

    @Override
    public List<PandaWorkbench> queryTodoDatial(PandaWorkbench todo) {
        return pandaWorkbenchMapper.selectPandaWorkbenchList(todo);
    }

    @Override
    public List<UserTodoReportDto> queryUserTodoReport() {
        List<PandaWorkbench> pandaWorkbenches = pandaWorkbenchMapper.selectUserTodoReport(Arrays.asList(Constants.OPERATOR_USER));
        List<UserTodoReportDto> userTodoReportDtos = JSON.parseArray(JSON.toJSONString(pandaWorkbenches), UserTodoReportDto.class);
        return userTodoReportDtos;
    }
}
